package com.micro;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.micro.communication.ApiServiceInterface;
import com.micro.communication.ApiServiceListener;
import com.micro.constant.IGeneral;
import com.micro.data.AccountInfo;

public class HomeActivity extends Activity implements ServiceConnection {

	

	Weibo weibo = new Weibo();
	List<HashMap<String, Object>> statusList = new ArrayList<HashMap<String, Object>>();
	private static int pageIndex = 1;
	ListView statusListView;
	AccountInfo account;
	
	private ApiServiceListener.Stub apiServiceListener = new ApiServiceListener.Stub() {

		@Override
		public void requestCompleted(String service, int type,
				String statusCode, String message) throws RemoteException {
			statusListView.setClickable(true);
			setProgressBarIndeterminateVisibility(false);
			if (statusCode != null && statusCode.equals("200")
					&& message != null && !message.equals("[null]")) {
				
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_sina);
		account = AccountInfo.loadAccount();
		statusListView = (ListView) findViewById(R.id.public_messagelist);
		final SimpleAdapter messageAdapter = new SimpleAdapter(this,
				statusList, R.layout.status_list, new String[] { "0", "1" },
				new int[] { R.id.status_public_msg,
						R.id.status_public_profile_pic });
		try {
			refreshMessageList();
		} catch (WeiboException e) {

			e.printStackTrace();
		}
		statusListView.setAdapter(messageAdapter);
		findViewById(R.id.refresh).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					refreshMessageList();
				} catch (WeiboException e) {
					e.printStackTrace();
				}
				messageAdapter.notifyDataSetChanged();
			}
		});

	}

	public void refreshMessageList() throws WeiboException {
		pageIndex++;
		for (Status status : weibo.getFriendsTimeline()) {
			if (!status.isRetweet()) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("0", status.getText());
				map.put("1", status.getUser().getProfileImageURL().toString());
				statusList.add(map);
				Log.d("cc", status.getUser().getProfileImageURL().toString());
			}
		}
	}

	private ApiServiceInterface apiServiceInterface;

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		apiServiceInterface = ApiServiceInterface.Stub.asInterface(service);
		statusListView.setClickable(false);
		setProgressBarIndeterminateVisibility(true);
		Map<String, Object> parameters;
		parameters = new HashMap<String, Object>();
		parameters.put("page", pageIndex);
		parameters.put("uid", account.getUid());
		try {
			apiServiceInterface.request(IGeneral.SERVICE_NAME_SINA, 0,
					apiServiceListener, parameters);
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		apiServiceInterface = null;
	}

}
